<template>
  <div>
    <el-dialog :title="info.title+'菜单'" :visible.sync="info.isShow" width="50%">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="类型">
          <el-radio-group v-model="ruleForm.type">
            <el-radio :label="1" :disabled="info.title=='修改'">目录</el-radio>
            <el-radio :label="2" :disabled="info.title=='修改'">菜单</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-show="ruleForm.type == 2" label="上级" prop="pid">
          <el-select v-model="ruleForm.pid">
            <el-option label="顶级目录" :value="0"></el-option>
            <el-option
              v-for="(item, index) in menuList"
              :key="index"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item v-show="ruleForm.type == 1" label="图标" prop="icon">
          <el-input v-model="ruleForm.icon"></el-input>
        </el-form-item>
        <el-form-item v-show="ruleForm.type == 2" label="地址" prop="url">
          <el-input v-model="ruleForm.url"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button class="btn" v-if="info.title === '添加'" type="primary" @click="submitForm('ruleForm')">立即添加</el-button>
          <el-button class="btn" v-if="info.title === '修改'" type="primary" @click="submitForm('ruleForm')">立即修改</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { addMenuFn, getMenuTreeListFn, editMenuFn } from "../../apis";
export default {
  props: ["info"],
  data() {
    return {
      ruleForm: {
        pid: 0,
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      },
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
      },
      // 下拉框的数组
      menuList: []
    };
  },
  created() {
    // 发请求，获取菜单
    this.getMenuList()
  },
  methods: {
    setValue(data) {
      // 设置弹窗中表单的值
      // 深拷贝，避免数据影响
      this.ruleForm = {...data}
    },
    async getMenuList() {
      let res = await getMenuTreeListFn();
      // 给下拉框的数组赋值
      this.menuList = res.list;
    },
    submitForm(formName) {
      // this.$refs[formName]获取表单的dom节点
      // validate 校验函数，element 自带的函数
      this.$refs[formName].validate((valid) => {
        // valid 校验的结果
        if (valid) {
          this.add();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 真的添加了
    async add() {
      let res;
      if(this.info.title === '添加') {
        res = await addMenuFn(this.ruleForm);
      }else {
        res = await editMenuFn(this.ruleForm);
      }
      if(res.code === 200){
        this.closed();
        // 通知list组件更新数据
        this.$emit('update');
        // 提示成功
        this.$message({
          type: "success",
          message: res.msg,
        });
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 关闭弹窗
    closed() {
      this.info.isShow = false;
    }
  },
};
</script>

<style>
</style>